//
//  SearchViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/7/31.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "SearchViewController.h"
#import "ReadyDiSignle.h"
#import "Base.h"
#import "Targets.h"
#import "DetailViewController.h"
#import "OrderMessagesignle.h"
@interface SearchViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UIView *searchView;
@property(nonatomic,strong)UITextField *textfield;
@property(nonatomic,strong)UIButton *searchButton;
@property(nonatomic,strong)UIScrollView *mainScroller;

@end

@implementation SearchViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
        self.view.backgroundColor=COLOR;
        }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
      [[ReadyDiSignle signle] addObserver:self forKeyPath:@"searchArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld  context:nil];
    [super viewWillAppear:animated];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
      [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"searchArray"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"订单搜索";
    [self setBack];
    self.searchView=[[UIView alloc] init];
    self.searchView.frame=CGRectMake(0, 64, WIDTH, 40);
    self.searchView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.searchView];
    
    self.textfield=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, WIDTH-40, 40)];
    
    self.textfield.delegate=self;
    self.textfield.placeholder=@"请输入订单号";
    self.textfield.textColor=[UIColor grayColor];
    [self.searchView addSubview:self.textfield];
    self.searchButton=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH-40, 0, 40, 40)];
    
        //[self.searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self.searchButton setImage:[UIImage imageNamed:@"searchios.png"] forState:UIControlStateNormal];
    self.searchButton.backgroundColor=[UIColor whiteColor];
    [self.searchView addSubview:self.searchButton];
    
    [self.searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    
     
    
       // Do any additional setup after loading the view.
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"searchArray"])
        {
        [self buju];
      
       }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)searchAction
{
    [self.mainScroller removeFromSuperview];
 
    [ReadyDiSignle signle].oidString=self.textfield.text;
    
    [[ReadyDiSignle signle] searchWorking];
  

}
-(void)buju
{
     self.mainScroller=[[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.searchView.frame)+5, WIDTH, HEIGHT-CGRectGetMaxY(self.searchView.frame))];
    self.mainScroller.backgroundColor=COLOR;
    [self.view addSubview:self.mainScroller];  
    for (int i=0; i<[ReadyDiSignle signle].searchArray.count; i++)
        {
        
        UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(0, 5+45*i, WIDTH, 40)];
        button.layer.cornerRadius=4;
        Targets *targets=[ReadyDiSignle signle].searchArray[i];
        [button setTitle:targets.oid forState:UIControlStateNormal];
        button.backgroundColor=[UIColor whiteColor];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTag:110+i];
        [button addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.mainScroller addSubview:button];
        

        }
    self.mainScroller.contentSize=CGSizeMake(0, [ReadyDiSignle signle].searchArray.count*45);
}
-(void)turnAction:(UIButton *)sender

{
    Targets *target=[ReadyDiSignle signle].searchArray[sender.tag-110];
    
    [OrderMessagesignle single].oidString=target._id;
    
    [[OrderMessagesignle single] orderMessagworking];
    
    
    DetailViewController *detailViewController=[[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailViewController animated:YES];

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
